<%@page import="exception.NotLoggedInException"%>
<%@page import="clientData.MemberDataInt"%>
<%@page import="java.util.LinkedList"%>
<%@page import="org.w3c.dom.Document"%>
<%@page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage=""%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.Import,java.rmi.*"%>
<%@page import="server.ForumHandlerInt"%>

<%@ include file="serverConnection.jsp" %>
<%
	
	String userName=(String) session.getAttribute("userName");
	String password=(String) session.getAttribute("password");
	if((userName==null)||(password==null)){
		response.sendRedirect("loggin.jsp");
		response.flushBuffer();
		return;
	}
	
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>friends management</title>


<%@ include file="scripts.jsp" %>

<script type="text/javascript">
	    	function addFriends(){
				// get the friend the user wants to add.
				var selectedIndex = document.getElementById("usersList").selectedIndex;
				if (selectedIndex == -1) {
					alert("please choose a friend to add");
				}
				else {
					var friendUserName = document.getElementById("usersList").options[selectedIndex].value;
					document.getElementById("friendsListDiv").innerHTML ="";
					// 	adding the friend to user friends list
					var xmlhttp;
					if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
						xmlhttp = new XMLHttpRequest();
					} else {// code for IE6, IE5
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
					xmlhttp.onreadystatechange = function() {
						
						if (xmlhttp.readyState == 4 && xmlhttp.status != 200)
						{
							document.write(xmlhttp.responseText);
						}
						
						if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {	
							document.getElementById("friendsListDiv").innerHTML = xmlhttp.responseText;
						}
					};
					xmlhttp.open("GET", "addFriend.jsp?friend=" + friendUserName, true);
					xmlhttp.send();
				}	
			}
		</script>
<script type="text/javascript">
	    
		function rmvFriend(){
			// get the username to remove from friends list.
			var selectedIndex = document.getElementById("friendsList").selectedIndex;
			if (selectedIndex == -1) {
				alert("please choose a friend to remove");
			}
			else {
				// get the username to remove from friend list
				var friendUserName = document.getElementById("friendsList").options[selectedIndex].value;
				var friendsListDiv = document.getElementById("friendsListDiv");
				friendsListDiv.innerHTML ="";
				// 	removing the user from the friends list
				var xmlhttp;
				if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				} else {// code for IE6, IE5
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.onreadystatechange = function() {
					
					if (xmlhttp.readyState == 4 && xmlhttp.status != 200)
					{
						document.write(xmlhttp.responseText);
					}
					
					if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {	
						friendsListDiv.innerHTML = xmlhttp.responseText;
					}
				};
				xmlhttp.open("GET", "removeFriend.jsp?friendToRemove=" + friendUserName, true);
				xmlhttp.send();
			}	
		}
	</script>
<script type="text/javascript">		
		function getUsers(str) {
			var xmlhttp;
			document.getElementById("usersListDiv").innerHTML ="";
			if (str.length == 0) {
				document.getElementById("usersListDiv").innerHTML = "<select id=\"usersList\" name=\"usersList\" size=\"10\" ></select>";
				return;
			}
			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp = new XMLHttpRequest();
			} else {// code for IE6, IE5
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status != 200)
				{
					document.write(xmlhttp.responseText);
				}
				
				
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {	
					
					document.getElementById("usersListDiv").innerHTML = xmlhttp.responseText;
				}
			};
			xmlhttp.open("GET", "getUsers.jsp?prefix=" + str, true);
			xmlhttp.send();
		}
	
	</script>


<!-- this script is notifing the user about his friends -->

<script type="text/javascript" src="jquery.js"></script>



<style type="text/css">
select {
	float: left;
	border-top-style: groove;
	border-right-style: groove;
	border-bottom-style: groove;
	border-left-style: groove;
	list-style-type: none;
	list-style-image: none;
	width: 120px;
	height: 200px;
}
</style>
    <link href="styles.css" rel="stylesheet" type="text/css">
    <link href="tableDesign.css" rel="stylesheet" type="text/css">
    <script language="JavaScript" type="text/JavaScript">
		<!--
		function MM_preloadImages() { //v3.0
		  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
			var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
		}
		-->
	</script>
</head>



















<body link="#6C8EBD">
<div id="main"> 
	<div id="header">
		<div id="logo"><img src="Images/logo1.jpg" alt="logo" width="490" height="182"></div>
		<div id="picture"><img src="Images/logo2.jpg" alt="pic" width="289" height="156"></div>
	</div>
	<div id="line"><img src="Images/line.jpg" alt="line" width="779" height="4"></div>
    <div id="navigation">
		FRIENDS MANAGEMENT
	</div>
	<div id="line2"><img src="Images/line.jpg" alt="line" width="779" height="4"></div>
	<div id="text" ice:editable="*">
		<div id="bar">
			<div class="bluetxt" id="linktxt">
            		<p><a href="mainScreen.jsp" class="bluetxt">main screen</a></p>
					<p><a href="index.jsp"  class="bluetxt" target="_top" onClick= "logoutFunc();">logout</a>  </p>
			</div>
		</div>
		<div id="text2">
			<div class="contentTXT" id="text2txt">
			  
<div id="friendsFormDiv">
		<form id="friendsForm" method="get" action="">
			<table id="friendTable" width="429" border="0">
				<tr>
					<th align="left" colspan="2" scope="row"><label> type user name prefix for searching user:</label>
					</th>
				</tr>
				<tr>
					<th align="left" colspan="2" scope="row"><input name="userPrefix"
						type="text" onkeyup="getUsers(this.value)" />
					</th>
				</tr>
				<tr>
					<th align="left" width="213" scope="row"><label> users found</label></th>
					<th align="left" width="206">friends list</th>
				</tr>
				<tr>
					<th scope="row">
						<div id="usersListDiv">
							<select id="usersList" name="usersList" size="10"></select>
						</div>
					</th>
					<td>
						<div id="friendsListDiv">
							<select name="friendsList" size="10" id="friendsList">
								<%
								
								LinkedList<MemberDataInt> friends = null;
							try
								{
								friends = forum.getUserFriends(userName,password);
								}
							catch(NotLoggedInException t)
							{
								response.sendRedirect("loggin.jsp");
								response.flushBuffer();
								return;
							}
								catch(SQLException s){
									   
									   
									   response.sendError(response.SC_INTERNAL_SERVER_ERROR,
										"SQL server is down");
									
										response.flushBuffer();
										return;
									   
									   
								   }
								   catch(RemoteException u){
									   
									   response.sendError(response.SC_INTERNAL_SERVER_ERROR,
										"forum server is down");
									
										response.flushBuffer();
										return;
								   }
								
							 
							for(MemberDataInt friend : friends){	
									%><option value="<%=friend.getUserName()%>"><%=friend.getUserName()%></option>
								<%
							}
							%>
							</select>
						</div></td>
				</tr>
				<tr>
					<td><input name="addFriend" type="button" value="add friend" onclick="addFriends()" /></td>
					<td><input name="removeFriend" type="button" value="remove friend" onclick="rmvFriend()" /></td>
				</tr>
			</table>
		</form>
	</div>
			</div>
		</div>
	</div>
<div id="Num"><img src="Images/line.jpg" alt="line" width="779" height="4"></div>
<div id="footer">
<div class="footerTXT" id="footertxt"><a class="footerTXT">Designed by ariel nir yuri gilad itzhaki and marina</a> </div>   </div>
</div>
</div>

</body>

</html>

